import express from 'express';
import createLog from "./middleware/createLog";
import bodyParser from 'body-parser';
import vaildPremission from './middleware/validPermission';
import database from "./model/database";
import mob from './routes/mob';
import manager from './routes/manager';
import {browserInit} from "./middleware/reptile";
import fs from "fs";
import {SimpleDateFormat} from "myjs-common";

let logPath = './log/conLog'
let logFile:any = null;
//控制台输出
console.log = function () {
    let logTime = SimpleDateFormat.formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss');
    let foldName = logTime.substr(0, 13);
    if (logTime != foldName) {
        logTime = foldName;
        let fname = logPath + foldName + '.log';
        if (logFile) {
            logFile.end();
        }
        // if (!fs.existsSync(fname)) {
        //     fs.mkdirSync(fname);
        // }
        logFile = fs.createWriteStream(fname, {
            flags: 'a',
            encoding: 'utf8'
        })
    }
    logFile.write('【' + logTime + '】' + arguments[0] + '\r\n')
}

const app = express();
//连接数据库
const db = database({
    user: "root",
    password: "WEB3g2019()",
    port: 3306,
    database: "3g",
    host: "121.4.127.126"
});
//跨域处理
app.use('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Content-Type,token');
    res.header("Access-Control-Expose-Headers", "token");
    res.header('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
    next();
})
app.use(vaildPremission);
app.use(createLog);
app.use(bodyParser.json());
browserInit().then(()=> {
    console.log("浏览器启动完毕！");
    mob(app, db);
    manager(app, db);
})

app.listen(3000)